package com.ip.student.service.impl;

import com.ip.student.models.service.IBaseClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;

@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 在学生服务中，我们创建一个默认的用户详情对象
        // 实际项目中，应该从数据库查询用户信息
        
        // 检查用户是否存在（简化实现）
        if (username == null || username.isEmpty()) {
            throw new UsernameNotFoundException("User not found with username: " + username);
        }

        // 设置默认权限
        String role = "USER";

        // 注意：在实际应用中，我们不应将真实密码存储在UserDetails中
        // 这里我们使用一个占位符，因为认证应该在登录时完成
        return new User(
                username,
                "", // 不存储真实密码
                Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + role))
        );
    }
}